package com.wind.taobao.api.trade.trade.convert
{
	import com.wind.taobao.api.trade.trade.model.Order;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Order转换器
	 * @author fxy
	 *
	 */
	public class OrderConvert
	{
		/**
		 * 转换Order集合
		 */
		public static function convertOrders(value:Object, type:String="json"):ArrayCollection
		{
			var orders:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							orders.addItem(convertOrder((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return orders;
		}

		/**
		 * 转换单个Order对象
		 * value:XML 或 JSON
		 */
		public static function convertOrder(value:Object, type:String="json", order:Order=null):Order
		{
			if (order == null)
				order=new Order();
			if (value == null)
				return order;
			if (type == TaobaoConstants.XML)
			{
				order=convertXMLToOrder(XML(value), order);
			}
			else
			{
				order=convertJSONToOrder(value, order);
			}
			return order;
		}

		/**
		 * JSON转换为Order
		 */
		private static function convertJSONToOrder(json:Object, order:Order):Order
		{
			if (json.hasOwnProperty(TaobaoConstants.IID))
			{
				order.iid=json[TaobaoConstants.IID];
			}
			if (json.hasOwnProperty(TaobaoConstants.SKU_ID))
			{
				order.sku_id=json[TaobaoConstants.SKU_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.SKU_PROPERTIES_NAME))
			{
				order.sku_properties_name=json[TaobaoConstants.SKU_PROPERTIES_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.ITEM_MEAL_NAME))
			{
				order.item_meal_name=json[TaobaoConstants.ITEM_MEAL_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.NUM))
			{
				order.num=json[TaobaoConstants.NUM];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				order.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PRICE))
			{
				order.price=json[TaobaoConstants.PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PIC_PATH))
			{
				order.pic_path=json[TaobaoConstants.PIC_PATH];
			}
			if (json.hasOwnProperty(TaobaoConstants.SELLER_NICK))
			{
				order.seller_nick=json[TaobaoConstants.SELLER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.BUYER_NICK))
			{
				order.buyer_nick=json[TaobaoConstants.BUYER_NICK];
			}
			if (json.hasOwnProperty(TaobaoConstants.TYPE))
			{
				order.type=json[TaobaoConstants.TYPE];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				order.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.REFUND_STATUS))
			{
				order.refund_status=json[TaobaoConstants.REFUND_STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.TID))
			{
				order.tid=json[TaobaoConstants.TID];
			}
			if (json.hasOwnProperty(TaobaoConstants.OUTERIID))
			{
				order.outer_iid=json[TaobaoConstants.OUTERIID];
			}

			if (json.hasOwnProperty(TaobaoConstants.OUTERSKUIDID))
			{
				order.outer_sku_id=json[TaobaoConstants.OUTERSKUIDID];
			}
			if (json.hasOwnProperty(TaobaoConstants.TOTAL_FEE))
			{
				order.total_fee=json[TaobaoConstants.TOTAL_FEE];
			}
			if (json.hasOwnProperty(TaobaoConstants.PAYMENT))
			{
				order.payment=json[TaobaoConstants.PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.DISCOUNT_FEE))
			{
				order.discount_fee=json[TaobaoConstants.PAYMENT];
			}
			if (json.hasOwnProperty(TaobaoConstants.ADJUST_FEE))
			{
				order.adjust_fee=json[TaobaoConstants.ADJUST_FEE];
			}

			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				order.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.SNAPSHOT_URL))
			{
				order.snapshot_url=json[TaobaoConstants.SNAPSHOT_URL];
			}
			if (json.hasOwnProperty(TaobaoConstants.TIMEOUT_ACTION_TIME))
			{
				order.timeout_action_time=json[TaobaoConstants.TIMEOUT_ACTION_TIME];
			}
			return order;
		}



		/**
		 * XML转换为Order
		 */
		private static function convertXMLToOrder(value:XML, order:Order):Order
		{
			return order;
		}

	}
}